package com.javaee.my.business;

import com.javaee.interceptor.entity.Shopping_Cart;
import com.javaee.interceptor.entity.goods;
import com.javaee.my.user.BaseControllerApi;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//删除购物车中的商品数据
@WebServlet("/deleteGoods")
public class deleteShoppingController extends HttpServlet implements BaseControllerApi {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        Integer goods_id = Integer.valueOf(req.getParameter("goods_id"));//获取商品的id
        HttpSession session = req.getSession();
        Integer id = (Integer) session.getAttribute("id");
        Shopping_Cart shopping_cart=new Shopping_Cart();
        shopping_cart.setGoods_id(goods_id);
        shopping_cart.setId(id);
        //根据商品的id进行商品的删除
        //1.首先判断购物车中是否有这个商品
        shopping_cart= sqlsession.selectOne("com.javaee.my.dao.businessMapper.check_goodsInShopping_cart",shopping_cart);
        if (shopping_cart==null)
        {
            req.getRequestDispatcher("404.jsp").forward(req,resp);
        }
        //然后根据商品id进行删除
        sqlsession.delete("com.javaee.my.dao.businessMapper.deleteGoods",shopping_cart);
        req.getRequestDispatcher("display.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
